package com.czk.aop.aspectj;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.StringJoiner;

/**
 * 定义自定义注解的切面类
 * @Author:ChenZhangKun
 * @Date: 2020/12/10 15:26
 */
@Component
@Aspect
public class MyLogAspect {
    /**
     * 相同切入点的抽取
     */
    @Pointcut(value = "execution(* com.czk.aop.entity.User.delete(..))")
    public void pointCommon(){
    }

    /**
     * 定义环绕通知
     * @param joinPoint
     */
    @Around("pointCommon()")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        // 拿到方法名称
        String name = joinPoint.getSignature().getName();
        // 拿到入参
        Object[] args = joinPoint.getArgs();
        // 拼接参数
        StringJoiner stringJoiner=new StringJoiner(",");
        // 遍历
        for (Object arg : args) {
            stringJoiner.add(arg.toString());
        }
        // 输出日志
        System.out.println("进入方法"+name+"的参数为"+stringJoiner.toString());
        joinPoint.proceed();
        System.out.println(name+"方法执行结束");
    }
}
